package com.manager.devicemanager.controller;

import com.alibaba.fastjson2.JSONObject;
import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.config.DeviceConfig;
import com.manager.devicemanager.entity.Realtimedevicestatus;
import com.manager.devicemanager.service.IDeviceInfoService;
import com.manager.devicemanager.service.IRealtimedevicestatusService;
import com.manager.devicemanager.service.ITestbatchService;
import com.manager.devicemanager.vo.ComponentStatusRequestVO;
import com.manager.devicemanager.vo.DeviceInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-06-24
 */
@RestController
@RequestMapping("/realtimedevicestatus")
public class RealtimedevicestatusController {
    @Autowired
    private IRealtimedevicestatusService realtimedevicestatusService;
    @Autowired
    private IDeviceInfoService iDeviceInfoService;
    @Autowired
    private DeviceConfig deviceConfig;
    @Autowired
    private ITestbatchService iTestbatchService;


    /**
     * 获取 IC 板的最新设备状态
     *
     * @return 实时设备状态对象
     */
    @PostMapping("/last")
    public Result<DeviceInfoVO> getDataLastIc(@RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
        try {
//            List<DeviceInfoVO> deviceInfoVOS = new ArrayList<>();
            DeviceInfoVO deviceInfoVO = new DeviceInfoVO();
            Realtimedevicestatus dataLastIc = realtimedevicestatusService.getDataLastIc("ic".equals(componentStatusRequestVO.getType())?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex());
            deviceInfoVO.setStorageStatus(dataLastIc == null || dataLastIc.getStorageStatus() == null ? "0" : dataLastIc.getStorageStatus());
            if (dataLastIc != null) {
                Date updateTime = dataLastIc.getUpdateTime();
                // 获取当前时间
                Date currentTime = new Date();
                // 计算时间差（毫秒）
                long diff = currentTime.getTime() - updateTime.getTime();
                // 5 分钟的毫秒数
                long fiveMinutesInMillis = deviceConfig.getMinutes() * 60 * 1000;
                // 判断时间差是否超过 5 分钟
                if (diff > fiveMinutesInMillis) {
                    // 设备离线逻辑，这里可以设置离线状态，例如添加一个 isOffline 属性到 DeviceInfoVO
                    deviceInfoVO.setIsOffline(true);
                } else {
                    deviceInfoVO.setIsOffline(false);
                }

            }
//            deviceInfoVOS.add(deviceInfoVO);

            return Result.success(deviceInfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取IC板最新设备状态失败");
        }
    }


    @PostMapping("/daycount")
    public Result<Long> countDaysWithRecords(@RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
        return  Result.success(iTestbatchService.getRunHours("ic".equals(componentStatusRequestVO.getType())?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex()));

//        return Result.success(realtimedevicestatusService.countDaysWithRecords(DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex()));
    }


}
